// pages/richPage/richPage.js
import {
  articleDetail
} from "../../service/jb_article_service"
import {
  BIND_USER_ID
} from "../../service/base_service"
import {
  loginWxAction
} from "../../service/config"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    knoData: {
      id: null,
    },
    loading: false,
    isShareEnter: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.knoData) {
      this.setData({
        knoData: app.globalData.knoData,
      })
      app.globalData.knoData = null;
    }
    if (options.id) {
      this.setData({
        'knoData.id': options.id,
        loading: true,
      })
    }

    if (options.isShare == 1) {
      this.setData({
        isShareEnter: true,
      })
      if (options.userId) {
        wx.setStorageSync(BIND_USER_ID, options.userId);
      }
      this.userWxLogin();
    } else {
      if (this.data.knoData.id) {
        this.queryKnoDetail();
      }
    }
    // 绑定分享参数
    wx.onCopyUrl(() => {
      return { query: `userId=${app.globalData.userId}&isShare=1` }
    })


  },
  onHide() {
    wx.offCopyUrl()
  },
  userWxLogin() {
    loginWxAction().then((res => {
      console.log("JJLog:登录返回:" + JSON.stringify(res));
      let data = res.data;
      if (res.code == 200) {
        app.globalData.nickName = data.nickname || "";
        app.globalData.avatar = data.avatar || "";
        app.globalData.phone = data.phone || "";
        app.globalData.isLogin = data.isLogin;
        app.globalData.userId = data.uid;
        this.queryKnoDetail();
      }
    })).catch(err => {
      console.error("JJLog:userWxLogin", err);

    })
  },

  queryKnoDetail() {
    if (this.data.loading) {
      wx.showLoading();
    }
    articleDetail(this.data.knoData.id).then(res => {
      if (this.data.loading) {
        wx.hideLoading();
      }
      if (res.code == 200) {
        this.setData({
          knoData: res.data,
        })
      }
    }).catch(err => {
      if (this.data.loading) {
        wx.hideLoading();
      }
    })
  },
  //跳转到商品详情
  pushToProduct(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/shopMallPackage/pages/newStoreDetail/newStoreDetail?houseNumber=' + item.id + "&type=normal",
    })
  },

  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = app.globalData.userId || ""
    let path = "/pages/richPage/richPage?isShare=1" + "&userId=" + userId + "&id=" + this.data.knoData.id
    return {
      title: this.data.knoData.title,
      path: path
    }
  }




})